package com.js.mail.member.service.impl;

import com.js.common.exception.PhoneExistException;
import com.js.common.exception.UsernameExistException;
import com.js.mail.member.dao.MemberLevelDao;
import com.js.mail.member.entity.MemberLevelEntity;
import com.js.mail.member.vo.UserLoginVo;
import com.js.mail.member.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.js.common.utils.PageUtils;
import com.js.common.utils.Query;

import com.js.mail.member.dao.MemberDao;
import com.js.mail.member.entity.MemberEntity;
import com.js.mail.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(UserRegistVo userRegistVo) {
        MemberEntity memberEntity = new MemberEntity();

        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();

        memberEntity.setLevelId(memberLevelEntity.getId());

        //检查用户名和手机号是否唯一
        checkPhoneUnique(userRegistVo.getPhone());
        checkUsernameUnique(userRegistVo.getUsername());

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(userRegistVo.getPassword());
        memberEntity.setPassword(encode);

        baseMapper.insert(memberEntity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        int mobile = this.count(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (mobile > 0) {
            throw new PhoneExistException();
        }

    }


    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException {
        int count = this.count(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count > 0) {
            throw new UsernameExistException();
        }
    }

    @Override
    public MemberEntity login(UserLoginVo userLoginVo) {

        String loginacct = userLoginVo.getLoginacct();
        String password = userLoginVo.getPassword();
        //数据库查询
        MemberEntity memberEntity = this.getOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().
                eq("mobile", loginacct));

        if (memberEntity == null) {
            return null;
        } else {
            //查询密码
            String passwordDb = memberEntity.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(password, passwordDb);
            if (matches) {
                return memberEntity;
            }else {
                return null;
            }
        }
    }

}